//
//  TableParentVC.m
//  NavTab
//
//  Created by  on 13-1-29.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "TablePullFreshParentVC.h"

@implementation TablePullFreshParentVC
@synthesize dataArray,tableView,style;
@synthesize par;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}
#pragma mark - 子类重写
-(void)getData{
    
}

-(void)getMoreData{
}

-(void)handleFooterPullFresh{
    //页面内容不足，不要上啦刷新。调整上拉刷新的位置
    if (self.tableView.contentSize.height >= self.tableView.frame.size.height){
        [self addFooterPullFresh];
    }else{
        [self removeFooterView];
    }
}

#pragma mark - 数据请求结束后的收尾
 
//更新结束，下拉还原
- (void)doneLoadingTableViewData{
	_reloading = NO;
    if (_refreshHeaderView) {
        [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
    }
    if (_refreshFooterView) {
        [_refreshFooterView egoRefreshScrollViewDataSourceDidFinishedLoading:self.tableView];
        [self handleFooterPullFresh];
    }
    
}


#pragma mark 下拉与上拉刷新

-(void)showRefreshHeader:(BOOL)animated{
    NSLog(@"wowpwpwpwpwpwppwwpwpwpw");
    [UIView animateWithDuration:animated?0.3:0 animations:^{
        self.tableView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
        [self.tableView scrollRectToVisible:CGRectMake(0, 0.0f, 1, 1) animated:NO];
        }
        completion:^(BOOL finished){
            [_refreshHeaderView setState:EGOOPullRefreshLoading];
            
            if ([self respondsToSelector:@selector(egoRefreshTableHeaderDidTriggerRefresh:)]) {
                [self egoRefreshTableHeaderDidTriggerRefresh:EGORefreshHeader];
            }

            
        }];
}

//添加下拉刷新
-(void)addHeaderPullFresh{
    //    return;
    
    EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.tableView.bounds.size.height, self.view.frame.size.width, self.tableView.bounds.size.height)];
    view.backgroundColor=self.view.backgroundColor;
    view.delegate = self;
    [self.tableView addSubview:view];
    _refreshHeaderView = view;
    //    [view release];
    [_refreshHeaderView refreshLastUpdatedDate];
    
}

//添加上拉刷新
-(void)addFooterPullFresh{

    //这50就是最后一行的高度
    
    //加载到最最底下
    CGFloat height = MAX(self.tableView.contentSize.height-VISIBLEFRESHFOOTER_HEIGHT, self.tableView.frame.size.height);
    if (_refreshFooterView && [_refreshFooterView superview]) {
        // reset position
        _refreshFooterView.frame = CGRectMake(0.0f,
                                              height,
                                              self.tableView.frame.size.width,
                                              self.view.bounds.size.height);
    }else {
        // create the footerView
        _refreshFooterView = [[EGORefreshTableFooterView alloc] initWithFrame:
                              CGRectMake(0.0f, height,
                                         self.tableView.frame.size.width, self.view.bounds.size.height)];
        _refreshFooterView.delegate = self;
        [self.tableView addSubview:_refreshFooterView];
    }
    
    if (_refreshFooterView) {
        [_refreshFooterView refreshLastUpdatedDate];
    }
}

-(void)removeFooterView{
    if ([_refreshFooterView superview]) {
        [_refreshFooterView removeFromSuperview];
    }
    _refreshFooterView=nil;
    
}

-(void)removeHeaderView{
    if ([_refreshHeaderView superview]) {
        [_refreshHeaderView removeFromSuperview];
    }
    _refreshHeaderView=nil;
    
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    CGRect rect=self.view.bounds;
    rect.origin.y=0;
    
    if (!tableView) {
        tableView=[[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain];
        tableView.delegate=self;
        tableView.dataSource=self;
        [self.view addSubview:tableView];
        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor=[UIColor clearColor];
        tableView.backgroundView=nil;
    }
    
    [self addHeaderPullFresh];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    DLog(@"section count=%d", [dataArray count] + isHavingMore?1:0);
    // Return the number of rows in the section.
    return [dataArray count] + (isHavingMore?1:0);
}



- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellActivity = @"CellActivity";
    UITableViewCell *cell=[_tableView dequeueReusableCellWithIdentifier:CellActivity];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellActivity];
    }
    cell.backgroundColor=[UIColor greenColor];
    cell.textLabel.text=@"hello";
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}



#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshPos)aRefreshPos{
    
    [self reloadTableViewDataSource];
    
    if (aRefreshPos==EGORefreshHeader) {
        //重新刷新数据
        freshPos=EGORefreshHeader;
        //注意:下拉刷新的时候必须删除上拉刷新，ego的bug。原因暂未知。
        [self removeFooterView];

        [self getData];
    }else{
        //加载更多数据
        freshPos=EGORefreshFooter;
        [self getMoreData];
    }
}


//检测当前是否正在更新网络数据
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(UIView*)view{
	
	return _reloading; // should return if data source model is reloading
	
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(UIView*)view{
	
	return [NSDate date]; // should return date data source was last changed
	
}

- (void)reloadTableViewDataSource{
    _reloading = YES;
}


#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

	if (_refreshFooterView) {
        [_refreshFooterView egoRefreshScrollViewDidScroll:scrollView];
    }
    if (_refreshHeaderView) {
        [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    }

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
	[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    [_refreshFooterView egoRefreshScrollViewDidEndDragging:scrollView];
    
    
}

@end
